package jsu.yym.tiktok.controller;

import jsu.yym.tiktok.pojo.*;
import jsu.yym.tiktok.mapper.FileMapper;
import jsu.yym.tiktok.mapper.FolderVideoMapMapper;
import jsu.yym.tiktok.mapper.VideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@CrossOrigin
@RestController
public class FolderVideoMapController {
    @Autowired
    private FolderVideoMapMapper folderVideoMapMapper;
    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private VideoMapper videoMapper;

    //返回用户收藏的所有视频
    @GetMapping("/collection/get")
    public ResponseResult getCollection()
    {
        String id = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null)
            id = authentication.getName(); // 获取当前用户的id
        //获取用户所有收藏夹id
        List<Integer> FileList = fileMapper.getPkIDByUsername(id);
        if (FileList.isEmpty()) {
            return new ResponseResult(409,"暂时还没有收藏视频");
        }
        //获取所有收藏视频的id
        List<Integer> PkIdList = folderVideoMapMapper.getVideoIdByPkIDs(FileList);
        if (PkIdList.isEmpty()) {
            return new ResponseResult(409,"暂时还没有收藏视频");
        }
        //获取对应id的视频对象
        List<Video> videos = videoMapper.getVideosByPkIds(PkIdList);
        if(videos.isEmpty())
            return new ResponseResult(503,"错误");
        return new ResponseResult(200,"成功",videos);
    }

    //用户点击收藏
    @PostMapping("/collection/add")
    public ResponseResult addCollection(@RequestBody FolderVideoMap folderVideoMap)
    {
        //判断是否重复收藏
        FolderVideoMap f = folderVideoMapMapper.getVideoMap(folderVideoMap.getFile_id(),folderVideoMap.getVideo_id());
        if (!Objects.isNull(f)) {
            return new ResponseResult(409,"重复添加");
        }
        System.out.println(folderVideoMap.getVideo_id()+"   "+folderVideoMap.getFile_id());
        //存入数据库
       folderVideoMapMapper.insertFolderVideoMap(folderVideoMap);
        //更新相关视频数据
        Video video = videoMapper.getVideoByPkId(folderVideoMap.getVideo_id());
        int count2 = videoMapper.updateCollectionsByPkId(video.getCollections()+1,video.getPkId());
        if(count2<=0)
            return new ResponseResult(403,"收藏失败");
        return new ResponseResult(200,"收藏成功");

    }

    //用户取消收藏
    @PostMapping("/collection/delete/{videoId}")
    public ResponseResult deleteCollection(@PathVariable int videoId)
    {
        String id = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null)
            id = authentication.getName(); // 获取当前用户的id
        System.out.println(videoId);
        FolderVideoMap folderVideoMap = new FolderVideoMap();
        //根据用户id获取用户所有收藏夹
        List<File> files = fileMapper.getFileByUsername(id);
        for (File f:files) {
            //根据文件夹id获取这个文件夹下的所有视频id
            List<Integer> idList = folderVideoMapMapper.getVideoIdByFileId(f.getPkId());
            for (Integer a:idList) {
                System.out.println(a);
                if(a==videoId)//找到对应的文件夹了
                {
                    folderVideoMap.setVideo_id(videoId);
                    folderVideoMap.setFile_id(f.getPkId());
                }
            }
        }
        System.out.println(folderVideoMap.toString());
       folderVideoMapMapper.deleteFolderMapVideo(folderVideoMap.getFile_id(),folderVideoMap.getVideo_id());
        //更新相关视频数据
        Video video = videoMapper.getVideoByPkId(folderVideoMap.getVideo_id());
        int count2 = videoMapper.updateCollectionsByPkId(video.getCollections()-1,video.getPkId());
        if(count2<=0)
            return new ResponseResult(503,"取消失败");
        return new ResponseResult(200,"取消成功");

    }




    /**
     * 获取对应文件夹下收藏的视频  文件夹id
     * @param id
     * @return
     */
    @GetMapping("/collection/get/{id}")
    public ResponseResult getCollectionByFileId(@PathVariable int id)
    {
        //获取对应的video_id集合
        List<Integer> list = folderVideoMapMapper.getVideoIdByFileId(id);
        if (list.isEmpty()) {
            return new ResponseResult(503,"此收藏夹下还没有视频");
        }
        //获取对应的video集合
        List<Video> videos = videoMapper.getVideosByPkIds(list);
        if (Objects.isNull(videos)) {
            return new ResponseResult(503,"此收藏夹下还没有视频");
        }
        return new ResponseResult(200,"获得成功",videos);
    }


    //将视频从一个文件夹下移到另一个文件夹下
    @PostMapping("/collection/move")
    public ResponseResult moveCollection(@RequestBody FileMove fileMove)
    {
        int count = folderVideoMapMapper.updateFileId(fileMove.getVideoId(),fileMove.getFormFileID(),fileMove.getToFileID());
        if(count<=0)
            return new ResponseResult(503,"移动失败");
        return new ResponseResult(200,"移动成功");
    }
}
